package com.kantboot.business.gc.repository;

import com.kantboot.business.gc.domain.dto.BusGcCollectSearchUserDTO;
import com.kantboot.business.gc.domain.entity.BusGcCollect;
import com.kantboot.business.gc.domain.vo.BusGcGameVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BusGcCollectRepository extends JpaRepository<BusGcCollect, Long> {

//    List<BusGcCollect> findByUserId(Long userId);

    BusGcCollect findByUserIdAndGameId(Long userId, Long gameId);

//    /**
//     * 分页查询用户收藏的游戏
//     */
//    List<BusGcCollect> findByUserId(Long userId, Integer page, Integer size);

    @Query("""
            SELECT t FROM BusGcCollect t
            WHERE
            (:#{#param.userId} IS NULL OR t.userId = :#{#param.userId})
            OR (:#{#param.keyword} IS NULL OR :#{#param.keyword} = '' 
            OR t.game.enName LIKE CONCAT('%',:#{#param.keyword},'%')
            OR t.game.zhName LIKE CONCAT('%',:#{#param.keyword},'%')
            OR t.game.zhDescription LIKE CONCAT('%',:#{#param.keyword},'%')
            OR t.game.enDescription LIKE CONCAT('%',:#{#param.keyword},'%')
            )
            ORDER BY t.gmtModified DESC
            """)
    Page<BusGcCollect> findBy(
            @Param("param") BusGcCollectSearchUserDTO param, Pageable pageable);

    //按gameId查询时间正序
    @Query("""
            SELECT t FROM BusGcCollect t
            WHERE
            t.gameId = :gameId
            ORDER BY t.gmtModified ASC
            """)
    List<BusGcCollect> findAllByGameId(Long gameId);

    @Query("""
            FROM BusGcGameVO a
            LEFT JOIN BusGcCollect b ON a.id = b.gameId
            WHERE
            (:#{#param.userId} IS NULL OR :#{#param.userId} = '' OR b.userId = :#{#param.userId})
             AND (:#{#param.zhName} IS NULL OR :#{#param.zhName} = '' OR a.zhName = :#{#param.zhName})
            AND (:#{#param.enName} IS NULL OR :#{#param.enName} = '' OR a.enName = :#{#param.enName})
            AND (:#{#param.zhDescription} IS NULL OR :#{#param.zhDescription} = '' OR a.zhDescription = :#{#param.zhDescription})
            AND (:#{#param.enDescription} IS NULL OR :#{#param.enDescription} = '' OR a.enDescription = :#{#param.enDescription})
            ORDER BY a.priority DESC
            """)
    Page<BusGcGameVO> getBodyDataByUserId(@Param("param") BusGcCollectSearchUserDTO param, Pageable pageable);
}
